package com.tsingteng.cosfun.ui.cosfun;

import android.content.Intent;
import android.media.AudioManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.dingmouren.layoutmanagergroup.viewpager.OnViewPagerListener;
import com.dingmouren.layoutmanagergroup.viewpager.ViewPagerLayoutManager;
import com.pili.pldroid.player.IMediaController;
import com.pili.pldroid.player.PLOnErrorListener;
import com.pili.pldroid.player.PLOnInfoListener;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseActivity;
import com.tsingteng.cosfun.ui.cosfun.adapter.PlayAdapter;
import com.tsingteng.cosfun.utils.AVOptionsUtil;
import com.tsingteng.cosfun.widget.dialog.ShowNetStatusDialog;

public class XiPuVideoPlayActivity extends BaseActivity {
    private String TAG = this.getClass().getSimpleName();
    private RecyclerView recyclerView = null;
    private ViewPagerLayoutManager mLayoutManager;
    private PlayAdapter mAdapter;

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.activity_play_main_recycler);
        mLayoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
        mAdapter = new PlayAdapter(this);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter);
        bright();
        initListener();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_play_main;
    }

    @Override
    protected void getIntentData(Intent intent) {

    }

    private void initListener() {
        mLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onPageRelease(boolean isNext, int position) {
                int index = 0;
                if (isNext) {
                    index = 0;
                } else {
                    index = 1;
                }
                releaseVideo(index);
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                playVideo();
            }

            @Override
            public void onLayoutComplete() {
            }

        });
    }

    public void playVideo() {
        View itemView = recyclerView.getChildAt(0);
        if (itemView == null) {
            Log.e("test12", "return");
            return;
        }
        Log.e("test12", "next");
        final PLVideoTextureView videoMain = itemView.findViewById(R.id.play_item_view_video_main);
        final PLVideoTextureView videoSecond = itemView.findViewById(R.id.play_item_view_video_second);
        final ImageView imgPlay = itemView.findViewById(R.id.play_item_view_play);
        videoMain.setAVOptions(AVOptionsUtil.getAVOptions());
        videoSecond.setAVOptions(AVOptionsUtil.getAVOptions());
        videoMain.setLooping(true);
        videoSecond.setLooping(true);
        videoMain.setOnInfoListener(mOnInfoListener);
        videoMain.setOnErrorListener(mOnErrorListener);
        videoMain.start();
        videoSecond.start();
        videoSecond.setVolume(0, 0);
        imgPlay.setOnClickListener(new View.OnClickListener() {
            boolean isPlaying = true;

            @Override
            public void onClick(View v) {
                if (videoMain.isPlaying()) {
                    imgPlay.animate().alpha(1f).start();
                    videoMain.pause();
                    videoSecond.pause();
                    isPlaying = false;
                } else {
                    imgPlay.animate().alpha(0f).start();
                    videoMain.start();
                    videoSecond.start();
                    isPlaying = true;
                }
            }
        });
    }

    public void releaseVideo(int index) {
        View itemView = recyclerView.getChildAt(index);
        if (itemView == null) {
            return;
        }
        final PLVideoTextureView videoMian = itemView.findViewById(R.id.play_item_view_video_main);
        final PLVideoTextureView videoSecond = itemView.findViewById(R.id.play_item_view_video_second);
        final ImageView imgPlay = itemView.findViewById(R.id.play_item_view_play);
        videoMian.stopPlayback();
        videoSecond.stopPlayback();
        imgPlay.animate().alpha(0f).start();
    }

    private void startOrPauseVideo() {
        View itemView = recyclerView.getChildAt(0);
        if (itemView == null) {
            return;
        }
        final PLVideoTextureView videoMian = itemView.findViewById(R.id.play_item_view_video_main);
        final PLVideoTextureView videoSecond = itemView.findViewById(R.id.play_item_view_video_second);
        final ImageView imgPlay = itemView.findViewById(R.id.play_item_view_play);
        if (videoMian.isPlaying()) {
            videoMian.pause();
            videoSecond.pause();
        } else {
            videoMian.start();
            videoSecond.start();
        }

    }

    private PLOnInfoListener mOnInfoListener = new PLOnInfoListener() {
        @Override
        public void onInfo(int what, int extra) {
            Log.i(TAG, "OnInfo, what = " + what + ", extra = " + extra);
            switch (what) {
                case PLOnInfoListener.MEDIA_INFO_BUFFERING_START:
                    break;
                case PLOnInfoListener.MEDIA_INFO_BUFFERING_END:
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_RENDERING_START:
                    break;
                case PLOnInfoListener.MEDIA_INFO_AUDIO_RENDERING_START:
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_FRAME_RENDERING:
                    Log.i(TAG, "video frame rendering, ts = " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_AUDIO_FRAME_RENDERING:
                    Log.i(TAG, "audio frame rendering, ts = " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_GOP_TIME:
                    Log.i(TAG, "Gop Time: " + extra);
                    break;
                case PLOnInfoListener.MEDIA_INFO_SWITCHING_SW_DECODE:
                    Log.i(TAG, "Hardware decoding failure, switching software decoding!");
                    break;
                case PLOnInfoListener.MEDIA_INFO_METADATA:
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_BITRATE:
                case PLOnInfoListener.MEDIA_INFO_VIDEO_FPS:
//                    updateStatInfo();
                    break;
                case PLOnInfoListener.MEDIA_INFO_CONNECTED:
                    Log.i(TAG, "Connected !");
                    break;
                case PLOnInfoListener.MEDIA_INFO_VIDEO_ROTATION_CHANGED:
                    Log.i(TAG, "Rotation changed: " + extra);
                default:
                    break;
            }
        }
    };
    private PLOnErrorListener mOnErrorListener = new PLOnErrorListener() {
        @Override
        public boolean onError(int errorCode) {
            Log.e(TAG, "Error happened, errorCode = " + errorCode);
            switch (errorCode) {
                case PLOnErrorListener.ERROR_CODE_IO_ERROR:
                    /**
                     * SDK will do reconnecting automatically
                     */
                    Log.e(TAG, "IO Error!");
                    return false;
                case PLOnErrorListener.ERROR_CODE_OPEN_FAILED:
                    Log.i(TAG, "failed to open player !");
                    break;
                case PLOnErrorListener.ERROR_CODE_SEEK_FAILED:
                    Log.i(TAG, "failed to seek !");
                    break;
                default:
                    Log.i(TAG, "unknown error !");
                    break;
            }
            return true;
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        startOrPauseVideo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        releaseVideo(0);
    }
}
